package com.ty.thymeleaf.controller;

import com.ty.thymeleaf.entity.User;
import com.ty.thymeleaf.service.UserService;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;



/**
 * LoginController
 *
 * @aurhor Administrator  whs
 * @since 2024/9/6
 */
@Controller
public class IndexController {

    @Resource
    private UserService userService;

    @RequestMapping(value = "/login")
    public String login(String usrName , String usrPassword, Model model, HttpSession session) {
        User user = userService.login(usrName, usrPassword);
        if (user != null) {
            session.setAttribute("loginUser", user);
            return "main";
        }
        model.addAttribute("message","用户名或密码错误，登录失败！");
        return "login";
    }

    @GetMapping(value = "/login")
    public String login() {
        return "login";
    }




}
